package com.ruoyi.project.mgr.clazz.controller;

import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.mgr.clazz.domain.MgrClazz;
import com.ruoyi.project.mgr.clazz.service.IMgrClazzService;
import com.ruoyi.project.system.user.domain.User;
import com.ruoyi.project.system.user.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 班级Controller
 * 
 * @author .
 * @date 2020-02-09
 */
@Controller
@RequestMapping("/mgr/clazz")
public class MgrClazzController extends BaseController
{
    private String prefix = "mgr/clazz";

    @Autowired
    private IMgrClazzService mgrClazzService;

    @Autowired
    private IUserService userService;

    @GetMapping()
    public String clazz()
    {
        return prefix + "/clazz";
    }

    /**
     * 查询班级列表
     */
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(MgrClazz mgrClazz)
    {
        startPage();
        List<MgrClazz> list = mgrClazzService.selectMgrClazzList(mgrClazz);
        return getDataTable(list);
    }

    /**
     * 导出班级列表
     */
    @Log(title = "班级", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(MgrClazz mgrClazz)
    {
        List<MgrClazz> list = mgrClazzService.selectMgrClazzList(mgrClazz);
        ExcelUtil<MgrClazz> util = new ExcelUtil<MgrClazz>(MgrClazz.class);
        return util.exportExcel(list, "clazz");
    }

    /**
     * 新增班级
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存班级
     */
    @Log(title = "班级", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(MgrClazz mgrClazz)
    {
        return toAjax(mgrClazzService.insertMgrClazz(mgrClazz));
    }

    /**
     * 修改班级
     */
    @GetMapping("/edit/{clazzId}")
    public String edit(@PathVariable("clazzId") Long clazzId, ModelMap mmap)
    {
        MgrClazz mgrClazz = mgrClazzService.selectMgrClazzById(clazzId);
        mmap.put("mgrClazz", mgrClazz);
        return prefix + "/edit";
    }

    /**
     * 修改保存班级
     */
    @Log(title = "班级", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(MgrClazz mgrClazz)
    {
        return toAjax(mgrClazzService.updateMgrClazz(mgrClazz));
    }

    /**
     * 删除班级
     */
    @Log(title = "班级", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(mgrClazzService.deleteMgrClazzByIds(ids));
    }

    /**
     * 修改班级
     */
    @GetMapping("/studentDetail/{clazzId}")
    public String studentDetail(@PathVariable("clazzId") Long clazzId, ModelMap mmap)
    {
        mmap.put("clazzId", clazzId);
        return prefix + "/student_list";
    }

    /**
     * 修改班级
     */
    @ResponseBody
    @PostMapping("/detail")
    public TableDataInfo detail(User user)
    {
        startPage();

        List<User> list = userService.selectUserList(user);
        return getDataTable(list);
    }
}
